<!DOCTYPE html>
<!--[if lte IE 8 ]>
<html class="ie" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<!--
***************  *      *     *
      8          *    *       *
      8          *  *         *
      8          **           *
      8          *  *         *
      8          *    *       *
      8          *      *     *
      8          *        *   ***********    -----Theme By Kieran(http://go.kieran.top)
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<!--<![endif]-->

<head>
  <title>linux tar | jack&#39;s blogs</title>
  <!-- Meta data -->
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" >
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="generator" content="jack's blogs">
    <meta name="author" content="Jack fan">
    <meta name="description" content="write something" />
    <meta name="keywords" content="jack's blogs" />

    <!-- Favicon, (keep icon in root folder) -->
    <link rel="Shortcut Icon" href="/img/favicon.ico" type="image/ico">

    <link rel="alternate" href="/atom.xml" title="jack&#39;s blogs" type="application/atom+xml">
    <link rel="stylesheet" href="/css/all.css" media="screen" type="text/css">
    
    <link rel="stylesheet" href="/highlightjs/vs.css" type="text/css">
    

    <!--[if IE 8]>
    <link rel="stylesheet" type="text/css" href="/css/ie8.css" />
    <![endif]-->

    <!-- jQuery | Load our jQuery, with an alternative source fallback to a local version if request is unavailable -->
    <script src="/js/jquery-1.11.1.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/jquery-1.11.1.min.js"><\/script>')</script>

    <!-- Load these in the <head> for quicker IE8+ load times -->
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="/js/html5shiv.min.js"></script>
    <script src="/js/respond.min.js"></script>
    <![endif]-->

  
  
  

  <style>.col-md-8.col-md-offset-2.opening-statement img{display:none;}</style>
</head>

<!--
<body class="post-template">
-->
<body id="index" class="lightnav animsition">

      <!-- ============================ Off-canvas navigation =========================== -->

    <div class="sb-slidebar sb-right sb-style-overlay sb-momentum-scrolling">
        <div class="sb-close" aria-label="Close Menu" aria-hidden="true">
            <img src="/img/close.png" alt="Close"/>
        </div>
        <!-- Lists in Slidebars -->
        <ul class="sb-menu">
            <li><a href="/" class="animsition-link" title="Home">Home</a></li>
            <li><a href="/archives" class="animsition-link" title="archive">archives</a></li>
            <!-- Dropdown Menu -->
			 
            <li>
                <a class="sb-toggle-submenu">Works<span class="sb-caret"></span></a>
                <ul class="sb-submenu">
                    
                        <li><a href="/tags/hexo" target="_BLANK" class="animsition-link">hexo</a></li>
                    
                        <li><a href="/tags/git" target="_BLANK" class="animsition-link">git</a></li>
                    
                </ul>
            </li>
            
            
        	<li>
        		<a class="sb-toggle-submenu">Categories<span class="sb-caret"></span></a>
            	<ul class="sb-submenu">
				  	
				    <li><a href="/categories/Reading/" class="animsition-link">Reading<small>(1)</small></a></li>
				    
				    <li><a href="/categories/git/" class="animsition-link">git<small>(1)</small></a></li>
				    
				    <li><a href="/categories/hexo/" class="animsition-link">hexo<small>(2)</small></a></li>
				    
				    <li><a href="/categories/linux/" class="animsition-link">linux<small>(1)</small></a></li>
				    
				    <li><a href="/categories/sql/" class="animsition-link">sql<small>(1)</small></a></li>
				    
				</ul>
        	</li>
			
            
            <li>
                <a class="sb-toggle-submenu">Links<span class="sb-caret"></span></a>
                <ul class="sb-submenu">
                    
                    <li><a href="http://go.kieran.top/" class="animsition-link">Kieran</a></li>
                    
                    <li><a href="http://domain.com/" class="animsition-link">Name</a></li>
                    
                </ul>
            </li>
            
        </ul>
        <!-- Lists in Slidebars -->
        <ul class="sb-menu secondary">
            <li><a href="/about.html" class="animsition-link" title="about">About</a></li>
            <li><a href="/atom.xml" class="animsition-link" title="rss">RSS</a></li>
        </ul>
    </div>
    
    <!-- ============================ END Off-canvas navigation =========================== -->

    <!-- ============================ #sb-site Main Page Wrapper =========================== -->

    <div id="sb-site">
        <!-- #sb-site - All page content should be contained within this id, except the off-canvas navigation itself -->

        <!-- ============================ Header & Logo bar =========================== -->

        <div id="navigation" class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <!-- Nav logo -->
                    <div class="logo">
                        <a href="/" title="Logo" class="animsition-link">
                         <img src="/img/logo.png" alt="Logo" width="35px;"/> 
                        </a>
                    </div>
                    <!-- // Nav logo -->
                    <!-- Info-bar -->
                    <nav>
                        <ul class="nav">
                            <li><a href="/" class="animsition-link">jack's blogs</a></li>
                            <li class="nolink"><span>Always </span>Creative.</li>
                            
                            <li><a href="https://github.com/fanyangyang" title="Github" target="_blank"><i class="icon-github"></i></a></li>
                            
                            
                            
                            
                            
                            <li class="nolink"><span>Welcome!</span></li>
                        </ul>
                    </nav>
                    <!--// Info-bar -->
                </div>
                <!-- // .container -->
                <div class="learnmore sb-toggle-right">More</div>
                <button type="button" class="navbar-toggle menu-icon sb-toggle-right" title="More">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar before"></span>
                <span class="icon-bar main"></span>
                <span class="icon-bar after"></span>
                </button>
            </div>
            <!-- // .navbar-inner -->
        </div>

        <!-- ============================ Header & Logo bar =========================== -->


      
<section id="intro">
    <div class="container">
        <div class="row col-md-offset-2">
            <div class="col-md-8">
    			<span class="post-meta">
      <time datetime="2017-11-04T05:54:32.000Z" itemprop="datePublished">
          2017-11-04
      </time>
    
    
    | 
    <a href='/tags/linux/'>linux</a>
    
    
</span>
                 | 阅读数:
                    <span id="/linux-tar.html" class="leancloud_visitors" data-flag-title="linux tar"></span>
                
                <h1>linux tar</h1>
            </div>
        </div>
        <div class="col-md-8 col-md-offset-2">
      		<h1 id="Linux-压缩指令【tar】"><a href="#Linux-压缩指令【tar】" class="headerlink" title="Linux 压缩指令【tar】"></a>Linux 压缩指令【tar】</h1><blockquote>
<p>摘自：<a href="http://www.cnblogs.com/52linux/archive/2012/03/04/2379738.html" target="_blank" rel="external">http://www.cnblogs.com/52linux/archive/2012/03/04/2379738.html</a></p>
</blockquote>
<p>参数：<br>-c  ：建立一个压缩档案的参数指令(create 的意思)；<br>-x  ：解开一个压缩档案的参数指令！<br>-t  ：查看 tarfile 里面的档案！<br>特别注意，在参数的下达中， c/x/t 仅能存在一个！不可同时存在！<br>因为不可能同时压缩与解压缩。<br>-z  ：是否同时具有 gzip 的属性？亦即是否需要用 gzip 压缩？<br>-j  ：是否同时具有 bzip2 的属性？亦即是否需要用 bzip2 压缩？<br>-v  ：压缩的过程中显示档案！这个常用，但不建议用在背景执行过程！<br>-f  ：使用档名，请留意，在 f 之后要立即接档名喔！不要再加参数！<br>例如使用『 tar -zcvfP tfile sfile 』就是错误的写法，要写成<br>『 tar -zcvPf tfile sfile 』才对喔！<br>-p  ：使用原档案的原来属性（属性不会依据使用者而变）<br>-P  ：可以使用绝对路径来压缩！<br>-N  ：比后面接的日期(yyyy/mm/dd)还要新的才会被打包进新建的档案中！<br>–exclude FILE：在压缩的过程中，不要将 FILE 打包！</p>
<h3 id="范例："><a href="#范例：" class="headerlink" title="范例："></a>范例：</h3><h4 id="范例一："><a href="#范例一：" class="headerlink" title="范例一："></a>范例一：</h4><p>将整个 /etc 目录下的档案全部打包成为 /tmp/etc.tar<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># tar -cvf /tmp/etc.tar /etc  &lt;==仅打包，不压缩！</span></div><div class="line"></div><div class="line">[root@linux ~]<span class="comment"># tar -czvf /tmp/etc.tar.gz /etc  &lt;==打包后，以 gzip 压缩</span></div><div class="line"></div><div class="line">[root@linux ~]<span class="comment"># tar -cjvf /tmp/etc.tar.bz2 /etc  &lt;==打包后，以 bzip2 压缩</span></div></pre></td></tr></table></figure></p>
<p>特别注意，在参数<em>f</em>之后的档案档名是自己取的，我们习惯上都用<em>.tar</em>来作为辨识。如果加<em>z</em>参数，则以<em>.tar.gz</em>或<em>.tgz</em>来代表<em>gzip</em>压缩过的 tar file. 如果加<em>j</em>参数，则以<em>.tar.bz2</em> 来作为附档名啊～*<br>上述指令在执行的时候，会显示一个警告讯息：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 『tar: Removing leading `/' from member names 』那是关于绝对路径的特殊设定。</span></div></pre></td></tr></table></figure></p>
<h4 id="范例二："><a href="#范例二：" class="headerlink" title="范例二："></a>范例二：</h4><p>查阅上述 /tmp/etc.tar.gz 档案内有哪些档案？<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># tar -tzvf /tmp/etc.tar.gz</span></div></pre></td></tr></table></figure></p>
<p>由于我们使用 gzip 压缩，所以要查阅该 tar file 内的档案时，就得要加上 <em>z</em> 这个参数了！这很重要的！</p>
<h4 id="范例三："><a href="#范例三：" class="headerlink" title="范例三："></a>范例三：</h4><p>将 /tmp/etc.tar.gz 档案解压缩在 /usr/local/src 底下<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># cd /usr/local/src</span></div><div class="line"></div><div class="line">[root@linux src]<span class="comment"># tar -xzvf /tmp/etc.tar.gz</span></div></pre></td></tr></table></figure></p>
<p>在预设的情况下，我们可以将压缩档在任何地方解开的！以这个范例来说，我先将工作目录变换到 /usr/local/src 底下，并且解开 /tmp/etc.tar.gz ，则解开的目录会在 /usr/local/src/etc 呢！另外，如果您进入 /usr/local/src/etc则会发现，该目录下的档案属性与 /etc/ 可能会有所不同喔！</p>
<h4 id="范例四："><a href="#范例四：" class="headerlink" title="范例四："></a>范例四：</h4><p>在 /tmp 底下，我只想要将 /tmp/etc.tar.gz 内的 etc/passwd 解开而已<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># cd /tmp</span></div><div class="line"></div><div class="line">[root@linux tmp]<span class="comment"># tar -xzvf /tmp/etc.tar.gz etc/passwd</span></div></pre></td></tr></table></figure></p>
<p>我可以透过 <em>tar -tzvf</em> 来查阅tarfile内的文件名称，如果单只要一个档案，就可以透过这个方式来下达！注意到 etc.tar.gz 内的根目录<em>/</em>是被拿掉了！</p>
<h4 id="范例五："><a href="#范例五：" class="headerlink" title="范例五："></a>范例五：</h4><p>将 /etc/ 内的所有档案备份下来，并且保存其权限！<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># tar -czvpf /tmp/etc.tar.gz /etc</span></div></pre></td></tr></table></figure></p>
<p>这个 -p 的属性是很重要的，尤其是当您要保留原本档案的属性时！</p>
<h4 id="范例六："><a href="#范例六：" class="headerlink" title="范例六："></a>范例六：</h4><p>在 /home 当中，比 2005/06/01 新的档案才备份<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># tar -N '2005/06/01' -czvf home.tar.gz /home</span></div></pre></td></tr></table></figure></p>
<h4 id="范例七："><a href="#范例七：" class="headerlink" title="范例七："></a>范例七：</h4><p>我要备份 /home, /etc ，但不要 /home/dmtsai<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># tar --exclude /home/dmtsai -czvf myfile.tar.gz /home/* /etc</span></div></pre></td></tr></table></figure></p>
<h4 id="范例八："><a href="#范例八：" class="headerlink" title="范例八："></a>范例八：</h4><p>将 /etc/ 打包后直接解开在 /tmp 底下，而不产生档案！<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@linux ~]<span class="comment"># cd /tmp</span></div><div class="line"></div><div class="line">[root@linux tmp]<span class="comment"># tar -cvf - /etc | tar -xvf -</span></div></pre></td></tr></table></figure></p>
<p>这个动作有点像是 cp -r /etc /tmp 啦～依旧是有其有用途的！要注意的地方在于输出档变成 -而输入档也变成 - ，又有一个 | 存在～这分别代表 standard output, standard input 与管线命令啦！这部分我们会在 Bash shell 时，再次提到这个指令跟大家再解释啰！</p>
<h3 id="tar如何解压文件到指定的目录？"><a href="#tar如何解压文件到指定的目录？" class="headerlink" title="tar如何解压文件到指定的目录？"></a>tar如何解压文件到指定的目录？</h3><p>tar czvf mysql.tar.gz /var/lib/mysql<br>压缩的文件，我想解压到当前目录下，而不是绝对路径下，该如何解压？<em>tar cxvf mysql.tar.gz</em> 的话，会覆盖 /var/lib/mysql 下全部文件吧？我现在就一个表坏了，想单独恢复一个表的资料，而不是恢复全部数据库，所以要解压到指定的目录下，再把那个表资料找不来恢复。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># tar zxvf mysql.tar.gz -C /home/aaa</span></div></pre></td></tr></table></figure></p>
<p><em>tar zxvf mysql.tar.gz -C /home/aaa</em>这样的话，解压后的目录结构是怎么样的？<br>/home/aaa/var/lib/mysql 吗？<br>试下就知道了….<br>归档里面的所有文件均是相对引用，归档解压缩（释放）在了当前目录。归档文件总是释放到当前目录，为的是防止破坏文件系统中重名的文件。所以，楼主的解压缩不会覆盖/var/lib/mysql，只会释放在当前目录先，即./var/lib/mysql如果想覆盖的话，在归档和解压时请使用-P参数：<br>tar zcvf mysql.tar.gz -P /var/lib/mysql<br>tar -Pzxvf mysql.tar.gz</p>

            <div class="clearfix"></div>
            <hr class="nogutter">
        </div>
        <nav class="pagination" role="pagination">
    
    <a class="pull-left" href="/better-sql-query.html" style="float: left;">
        ← better sql query
    </a>
    
    
    <a class="pull-right" href="/git忽略文件无效.html">
        git忽略文件无效  →
    </a>
    
</nav>

        <div class="duoshuo">




</div>
    </div>
</section>


      
<!-- ============================ Footer =========================== -->

<footer>
    <div class="container">
            <div class="copy">
                <p>
                    &copy; 2014<script>new Date().getFullYear()>2010&&document.write("-"+new Date().getFullYear());</script>, Content By Jack fan. All Rights Reserved.
                </p>
                <p>Theme By <a href="//go.kieran.top" style="color: #767D84">Kieran</a></p>
            </div>
            <div class="social">
                <ul>
                    
                    <li><a href="https://github.com/fanyangyang" title="Github" target="_blank"><i class="icon-github"></i></a>&nbsp;</li>
                    
                    
                    
                    
                    
                </ul>
            </div>
            <div class="clearfix"> </div>
        </div>
</footer>

<!-- ============================ END Footer =========================== -->
<script src="//cdn1.lncld.net/static/js/2.5.0/av-min.js"></script>
<script>
    var APP_ID = '6q2JbQpr6Q4zmzFzG74kS1kA-gzGzoHsz';
    var APP_KEY = '0G5Hg5OUFcKLlGy5v1EiBArs';
    AV.init({
        appId: APP_ID,
        appKey: APP_KEY
    });
    // 显示次数
    function showTime(Counter) {
        var query = new AV.Query("Counter");
        if($(".leancloud_visitors").length > 0){
            var url = $(".leancloud_visitors").attr('id').trim();
            // where field
            query.equalTo("words", url);
            // count 
            query.count().then(function (number) {
                // There are number instances of MyClass where words equals url.
                $(document.getElementById(url)).text(number?  number : '--');
            }, function (error) {
                // error is an instance of AVError.
            });
        }
    }
   // 追加pv
    function addCount(Counter) {
        var url = $(".leancloud_visitors").length > 0 ? $(".leancloud_visitors").attr('id').trim() : 'icafebolger.com';
        var Counter = AV.Object.extend("Counter");
        var query = new Counter;
        query.save({
            words: url
        }).then(function (object) {
        })
    }
    $(function () {
        var Counter = AV.Object.extend("Counter");
        addCount(Counter);
        showTime(Counter);
    });
</script>
      <!-- Load our scripts -->
<!-- Resizable 'on-demand' full-height hero -->
<script type="text/javascript">
    var resizeHero = function () {
        var hero = $(".cover,.heightblock"),
            window1 = $(window);
        hero.css({
            "height": window1.height()
        });
    };

    resizeHero();

    $(window).resize(function () {
        resizeHero();
    });
</script>
<script src="/js/plugins.min.js"></script><!-- Bootstrap core and concatenated plugins always load here -->
<script src="/js/jquery.flexslider-min.js"></script><!-- Flexslider plugin -->
<script src="/js/scripts.js"></script><!-- Theme scripts -->


<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$('#intro').find('img').each(function(){
  var alt = this.alt;

  if (alt){
    $(this).after('<span class="caption" style="display:none">' + alt + '</span>');
  }

  $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox" rel="gallery" />');
});
(function($){
  $('.fancybox').fancybox();
})(jQuery);
</script>

<!-- Initiate flexslider plugin -->
<script type="text/javascript">
    $(document).ready(function($) {
      (function(){
        console.log('font');
        var getCss = function(path) {
          var head = document.getElementsByTagName('head')[0];
          link = document.createElement('link');
          link.href = path;
          link.rel = 'stylesheet';
          link.type = 'text/css';
          head.appendChild(link);
        };
        getCss('https://fonts.googleapis.com/css?family=Montserrat:400,700');
        getCss('https://fonts.googleapis.com/css?family=Open+Sans:400,600');
      })();
      $('.flexslider').flexslider({
        animation: "fade",
        prevText: "",
        nextText: "",
        directionNav: true
      });
    });
</script>

</body>
</html>
